<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">单位名称</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.name"
						:placeholder="optionDisabled?'-':'请输入单位名称'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">联系人名称</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.contactName"
						:placeholder="optionDisabled?'-':'请输入联系人名称'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">联系人手机号</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.contactPhone"
						:placeholder="optionDisabled?'-':'请输入联系人手机号'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">单位地址</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.address"
						:placeholder="optionDisabled?'-':'请输入社会信用代码'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">食品经营许可证复印件图片或备案证明</text>
				<view class="data-form-input">
					<gui-upload-images2 @change="choosePhotoGwpz($event, 'fBLicense')" :header="header" fileName="file"
						ref="uploadfBLicense" @uploaded="uploadedGwpz($event, 'fBLicense')"
						:uploadServerUrl="`${$http.config.baseUrl}/common/upload`"
						:maxFileNumber="1"></gui-upload-images2>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">供餐合同/协议文件</text>
				<view class="data-form-input">
					<gui-upload-images2 @change="choosePhotoGwpz($event, 'contract')" :header="header" fileName="file"
						ref="uploadcontract" @uploaded="uploadedGwpz($event, 'contract')"
						:uploadServerUrl="`${$http.config.baseUrl}/common/upload`"
						:maxFileNumber="1"></gui-upload-images2>
				</view>
			</view>
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				dataForm: { //表单
					"id": '',
					"name": "",
					"contactName": "",
					"contactPhone": "",
					"address": "",
					"fBLicense": "",
					"contract": ""
				},
				header: {
					"Authorization": uni.getStorageSync('lifeData')?.vuex_token
				},
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			if (options.op != 'add') {
				this.loadDetail();
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			loadDetail() {
				this.$http.get(`/archives/unit/${this.did}`).then(res => {
					if (res.code == 200) {
						let {
							id,
							name,
							contactName,
							contactPhone,
							address,
							fBLicense,
							contract
						} = res.data;
						this.dataForm.id = id;
						this.dataForm.name = name;
						this.dataForm.contactName = contactName;
						this.dataForm.contactPhone = contactPhone;
						this.dataForm.address = address;
						this.dataForm.contract = contract;
						this.dataForm.fBLicense = fBLicense;
						
						this.$refs.uploadcontract.setItems([contract]);
						this.$refs.uploadfBLicense.setItems([fBLicense]);
					}
					console.log('', result)
				})
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				console.log('提交');
				if (!this.dataForm.name) return this.$u.msg('请输入供餐单位名称')
				if (!this.dataForm.contactName) return this.$u.msg('请输入联系人')
				if (!this.dataForm.contactPhone) return this.$u.msg('请输入联系人手机号')
				if (!this.dataForm.address) return this.$u.msg('请输入单位地址')
				if (!this.dataForm.fBLicense) return this.$u.msg('请上传食品经营许可证复印件图片或备案证明')
				if (!this.dataForm.contract) return this.$u.msg('请上传供餐合同/协议文件')
				if (this.dataForm.id) {
					this.$http.put('/archives/unit', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/archives/unit', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			},
			choosePhotoGwpz(e, name) {
				console.log('upload' + name);
				this.$refs['upload' + name].upload();
			},
			uploadedGwpz(uploadedImgs, name) {
				let arr = []
				for (var i = 0; i < uploadedImgs.length; i++) {
					arr.push(uploadedImgs[i].url)
				}
				this.dataForm[name] = arr.join();
			}
		}
	}
</script>

<style>

</style>